package com.hmall.api.config;

import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {

    //配置一个feign的请求拦截器；设置请求头中的user-info
    @Bean
    public RequestInterceptor requestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                Long userId = UserContext.getUser();
                if(userId != null) {
                    template.header("user-info", String.valueOf(UserContext.getUser()));
                }
            }
        };
    }


    //配置feign的日志记录级别；默认是none，没有日志输出
    @Bean
    public Logger.Level feignLoggerLevel() {
        //配置为full会在控制台输出详细的调用日志
        return Logger.Level.FULL;
    }
}
